package com.study;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GuestListener implements ServletContextListener {
	private Logger logger = LoggerFactory.getLogger(GuestListener.class);
	
	@Override
	public void contextInitialized(ServletContextEvent e) {
		logger.debug("contextInitialized");

		EntityManagerFactory emf = Persistence.createEntityManagerFactory("DEV-ORACLE");
		e.getServletContext().setAttribute("emf", emf);
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent e) {
		logger.debug("contextDestroyed");
		
		EntityManagerFactory emf = (EntityManagerFactory)e.getServletContext().getAttribute("emf");
		emf.close();
	}
}
